import BaseView from "../../../cfw/mvc/BaseView";
import CollisionHelper from "../../../cfw/collide/CollisionHelper";
import BathRoomController from "../BathRoomController";
import WaterStreamComp from "../../toilet/view/WaterStreamComp";
import BathItemView from "./BathItemView";
import BathItemModel from "../model/BathItemModel";
import BathRoomManager from "../model/BathRoomManager";
import SDKManager from "../../../sdk/SDKManager";
import { SDKEventName } from "../../../sdk/SDKConfig";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BathRoomView extends BaseView {

    @property([BathItemView])
    daoju: BathItemView[] = [];

    @property(cc.Node)
    waterLayer: cc.Node = null;


    @property(cc.Node)
    ruye: cc.Node = null;

    @property(cc.Node)
    paopao: cc.Node = null;

    @property(cc.Node)
    iceLayer: cc.Node = null;
    @property(WaterStreamComp)
    water: WaterStreamComp = null;

    @property(cc.Node)
    playerRect: cc.Node = null;

    protected clickIndex: number = -1;

    protected initPos: cc.Vec3 = cc.v3()

    protected waterNum: number = 0;

    protected controller: BathRoomController;
    protected model: BathRoomManager;

    protected clickIndexList: number[] = []
    start() {
        let list: BathItemModel[] = this.model.getList()
        for (let index = 0; index < this.daoju.length; index++) {
            const element = this.daoju[index];
            if (list[index]) {
                element.setModel(list[index])
            }
        }
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this)
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this)
        SDKManager.getChannel().sendEvent(SDKEventName.bathroom)
    }

    touchStart(e: cc.Touch) {
        if (this.clickIndex == -1) {
            let pos = e.getLocation();
            for (let index = 0; index < this.daoju.length; index++) {
                const element = this.daoju[index];
                if (element.getBoundingBoxToWorld().contains(pos)) {
                    // if (!element.isOpen()) {
                    //     TipController.instance().showToast(GameText.ITEM_TOAST, { num: element.getOpenLevel() })
                    //     return;
                    // }                    
                    cc.log(' element.needBuy() ', element.needBuy())
                    if (element.needBuy()) {
                        this.controller.openItem(element.getModel())

                    } else {
                        this.clickIndex = index;
                        this.initPos.x = this.daoju[this.clickIndex].x;
                        this.initPos.y = this.daoju[this.clickIndex].y;
                        cc.log(' 点击到 是 index ', this.clickIndex)
                        if (this.clickIndex == 0) {
                            this.water.setStartFlag(true)
                        }
                    }


                    break;
                }
            }
        }
    }

    touchMove(e: cc.Touch) {
        if (this.clickIndex >= 0) {
            let pos = e.getLocation();
            pos = this.node.convertToNodeSpaceAR(pos)
            this.daoju[this.clickIndex].x = pos.x;
            this.daoju[this.clickIndex].y = pos.y;
            this.collide(pos)
        }

    }


    collide(pos) {
        switch (this.clickIndex) {
            case 0://喷头

                this.collideHead(pos, this.paopao, 4)
                this.collideHead(pos, this.iceLayer, 4)
                this.collideHead(pos, this.ruye, 4)
                break;

            case 1://沐浴露
                this.collideWithRuye(pos, this.ruye, 256, 228, 8)
                break;
            case 2:
                this.collideWithRuye(pos, this.iceLayer, 256, 208, 8)
                break;
            case 3:
                this.collideWithRuye(pos, this.paopao, 256, 204, 8)
                break;
            case 4:

                break;
        }
    }

    collideHead(pos, element, step: number) {
        let rect = element.getBoundingBoxToWorld();
        // cc.log(' rect ', rect.x, rect.y, rect.width, rect.height)
        let rect2 = this.daoju[this.clickIndex].getBoundingBoxToWorld();
        // cc.log(' rect2 ', rect2.x, rect2.y, rect2.width, rect2.height)
        if (CollisionHelper.isRectCollideRect(rect, rect2)) {
            // cc.log('  update  ---------------------  ')
            let parent = element.parent;
            if (parent.width > 0) {
                parent.width -= step;
                if (parent.width < 0) {
                    parent.height = 0;
                }
            }
            if (parent.height > 0) {
                parent.height -= 0;
                if (parent.height < 0) {
                    parent.height = 0;
                }
            }
        }
    }


    updateWaterLayer() {
        if (this.waterNum > 0) {
            this.waterLayer.y -= 3;
            this.waterNum -= 3;
            if (this.waterNum < 0) {
                this.waterNum = 0;
            }
        }
    }

    update(dt: number) {
        if (this.clickIndex == 4) {
            let rect = this.playerRect.getBoundingBox()
            let pos = this.daoju[this.clickIndex].getPosition()
            // cc.log('this.playerRect.getBoundingBoxToWorld() ', rect.x, rect.y, rect.width, rect.height, ' pos ', pos.x, pos.y)
            if (rect.contains(pos)) {
                if (this.waterNum < this.waterLayer.height) {
                    this.waterLayer.y += 3;
                    this.waterNum += 3;

                }
            } else {
                this.updateWaterLayer();
            }


        } else {
            this.updateWaterLayer();
        }


    }


    collideWithRuye(pos, element, maxW, maxH, step: number) {
        // const element = this.ruye
        let rect = element.getBoundingBoxToWorld();
        // cc.log(' rect ', rect.x, rect.y, rect.width, rect.height)
        let rect2 = this.daoju[this.clickIndex].getBoundingBoxToWorld();
        // cc.log(' rect2 ', rect2.x, rect2.y, rect2.width, rect2.height)
        if (CollisionHelper.isRectCollideRect(rect, rect2)) {
            // cc.log('  update  ---------------------  ')
            let parent = element.parent;
            if (parent.width < maxW) {
                parent.width += step;
                if (parent.width >= maxW) {

                }
            }
            if (parent.height < maxH) {
                parent.height += step;
                if (parent.height >= maxH) {

                }
            }
        }
    }

    touchEnd() {
        if (this.clickIndex >= 0) {
            this.daoju[this.clickIndex].x = this.initPos.x;
            this.daoju[this.clickIndex].y = this.initPos.y;
            if (!this.clickIndexList[this.clickIndex]) {
                this.model.addNum(this.daoju[this.clickIndex].getModel())
                this.clickIndexList[this.clickIndex] = 1;
            }

            if (this.clickIndex == 0) {
                this.water.setStartFlag(false)
            }
            this.clickIndex = -1;
        }

    }

    onButtonBackClick() {
        this.controller.back();
    }

    onButtonNextClick() {
        this.model.updateExp()
        this.controller.gotoNext()
    }
    // update (dt) {}
}
